package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DeviceRepairRecordMapper;
import com.zhijian.medical.entity.po.DeviceRepairRecord;
import com.zhijian.medical.entity.vo.request.DeviceRepairRecordSearchVo;
import com.zhijian.medical.entity.vo.response.DeviceRepairRecordVo;
import com.zhijian.medical.service.DeviceRepairRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 维修记录业务处理层
 *
 * @author HCG
 * @date 2022-01-01 15:40:08
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceRepairRecordServiceImpl implements DeviceRepairRecordService {

    @Autowired
    private DeviceRepairRecordMapper mapper;

    @Override
    public DeviceRepairRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceRepairRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceRepairRecord deviceRepairRecord) {
        Date now = new Date();
        deviceRepairRecord.setCreateTime(now);
        deviceRepairRecord.setUpdateTime(now);
        return mapper.insert(deviceRepairRecord) == 1 ? deviceRepairRecord.getId() : 0L;
    }

    @Override
    public boolean update(DeviceRepairRecord deviceRepairRecord) {
        Date now = new Date();
        deviceRepairRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceRepairRecord) != 0;
    }

    @Override
    public Integer count(DeviceRepairRecordSearchVo deviceRepairRecordSearchVo) {
        return mapper.count(deviceRepairRecordSearchVo);
    }

    @Override
    public List<DeviceRepairRecordVo> selectList(DeviceRepairRecordSearchVo deviceRepairRecordSearchVo) {
        return mapper.selectList(deviceRepairRecordSearchVo);
    }

    @Override
    public Page<DeviceRepairRecordVo> selectPage(DeviceRepairRecordSearchVo deviceRepairRecordSearchVo) {
        //先直接查总数
        int count = count(deviceRepairRecordSearchVo);
        //分页
        Page<DeviceRepairRecordVo> page = new Page<>(true, count, deviceRepairRecordSearchVo.getPageNum(), deviceRepairRecordSearchVo.getPageSize());
        deviceRepairRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceRepairRecordVo> responseVos = this.selectList(deviceRepairRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

}